package firbi.base;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.TabHost.TabSpec;
import firbi.base.util.Constants;


public class Favorites extends ListActivity {
	
	//private static final int GET_HELP = Menu.FIRST;
	private static final int DELETE_FAV = 0;
	private static final int VIEW_FAV = 1;
	private static final int DIALOG_HELP = 2;
	private static final int CANCEL = 3;


	protected FavsDbAdapter mFavDbHelper;	
	protected Cursor mFavCursor;
	private Dialog mHelpD;//help dialog


	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.favorites);
        mFavDbHelper = new FavsDbAdapter(this);
        mFavDbHelper.open();
        populateList();
        setUpHelp();
		registerForContextMenu(getListView());
    }
    

    /**
     * Populates the menu for the activity
     */
   /* @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	menu.add(0,R.id.menu_item_help, 0, "Help");
		menu.add(0, R.id.menu_item_listsearch, 0, "Search in List Mode");
		menu.add(0,R.id.menu_item_mapsearch,0,"Search in Map Mode");
        return true;
    }
*/


	/*@Override

	public boolean onMenuItemSelected(int featureId, MenuItem item){
		
		Intent i;
	    switch (item.getItemId()) {
			case R.id.menu_item_mapsearch:
				i = new Intent(this, Search.class);
				i.putExtra("isMapView", true);
				startActivity(i);
				return true;
			case R.id.menu_item_listsearch:
				i = new Intent(this, Search.class);
				i.putExtra("isMapView", false);
				startActivity(i);
				return true;
			case R.id.menu_item_help:
				showDialog(DIALOG_HELP);
				return true;
		}
	    //return false;
		//switch(item.getItemId()){
		//	case DIALOG_HELP:
		//		showDialog(DIALOG_HELP);
		//		return true;
		//}
		return super.onMenuItemSelected(featureId, item);
	}
*/
    
    
    /**
     * Sets up the help dialog box.
     */
    private void setUpHelp() {
    	LayoutInflater li = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	View v = li.inflate(R.layout.favorites_help_dialog, null);
        
        TabHost helpTab = (TabHost)v.findViewById(R.id.tabhost);
        helpTab.setup();
        TabSpec ts = helpTab.newTabSpec("TAB_TAG_1"); 
        ts.setIndicator("Main");
        ts.setContent(R.id.favorites_help_tab_main);
        helpTab.addTab(ts);
        TabSpec ts2 = helpTab.newTabSpec("TAB_TAG_2"); 
        ts2.setIndicator("Menu");
        ts2.setContent(R.id.favorites_help_tab_menu);
        helpTab.addTab(ts2);
        
        mHelpD = new Dialog(this);
        Button b = (Button)v.findViewById(R.id.btnExit);
        b.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mHelpD.dismiss();
			}
        });
    	mHelpD.setTitle("Favorites Help");
    	mHelpD.setContentView(v);
    	mHelpD.setCanceledOnTouchOutside(true);
    }
    
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent i;
	    switch (item.getItemId()) {
			case R.id.menu_item_mapsearch:
				i = new Intent(this, Search.class);
				i.putExtra("isMapView", true);
				startActivity(i);
				return true;
			case R.id.menu_item_listsearch:
				i = new Intent(this, Search.class);
				i.putExtra("isMapView", false);
				startActivity(i);
				return true;
			case R.id.menu_item_help:
				//showDialog(DIALOG_HELP);
				mHelpD.show();
				return true;
		}
	    return false;
    }
    
    
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.favorites_menu, menu);
	    return true;
	}

	

	protected Dialog onCreateDialog(int id) {
		Dialog dialog;
		switch(id) {
			case DIALOG_HELP:
				// do the work to define the Help Section
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setMessage("Favorites Help\n\nTo view the current schedule information for a favorite click on the desired favorite row in the list.\n"+  

								   "To delete a favorite perform a \"long-press\" (press and hold for about two seconds) on the desired favoirite row in the list"+

								   "to reveal a floating menu that provides 'delete' and 'cancel' selections.\n"+
								   "Press 'cancel' is to go back to the favorites screen.")
					.setPositiveButton("OK", new DialogInterface.OnClickListener() {
								   public void onClick(DialogInterface dialog, int id) {
									   dialog.cancel();
								   }
								   });
				dialog = builder.create();
				break;
			default:
				dialog = null;
		}
		return dialog;
	}

	

	

    @Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		//menu.add(0, VIEW_FAV, 0, "View Current Info");
        menu.add(0, DELETE_FAV, 0, "Delete Favorite");
        menu.add(0,CANCEL, 0, "Cancel");
	}

    



	@Override

	public boolean onContextItemSelected(MenuItem item) {

		switch(item.getItemId()) {
			case CANCEL:
			    return true;
			case DELETE_FAV:
				AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
				mFavDbHelper.deleteFav(info.id);
				Toast.makeText(getApplicationContext(), "should delete the entry here", Toast.LENGTH_SHORT);
				populateList();
				return true;
				
			//changed view favorite to the short click so it seems sensless to have it here too for the long click	
			/*case VIEW_FAV:
				AdapterContextMenuInfo infoB = (AdapterContextMenuInfo) item.getMenuInfo();
				Cursor mCursor = mFavDbHelper.getFav(infoB.id);
				if(mCursor == null){
					Toast.makeText(getApplicationContext(),"Error:  Could not find favorite in the database", Toast.LENGTH_LONG);
					return false;
				}else{
					int route_col_index = mCursor.getColumnIndex(FavsDbAdapter.KEY_ROUTEID);
					int stop_col_index = mCursor.getColumnIndex(FavsDbAdapter.KEY_STOPID);
					
					Intent i = new Intent(this, StopView.class);
			        i.putExtra(Constants.sSTOP_KEY, mCursor.getString(stop_col_index));
			        i.putExtra(Constants.sROUTE_KEY, mCursor.getString(route_col_index));
					startActivity(i);
					
				return true;
				}*/
		}
		return super.onContextItemSelected(item);
	}

    //Gets all the favorites data from the db, creates an item list, and displays it
    private void populateList(){
		mFavCursor = mFavDbHelper.getAllFavs();
		Toast.makeText(getApplicationContext(), "populating list", Toast.LENGTH_SHORT);
		startManagingCursor(mFavCursor);
		String [] from = new String []{FavsDbAdapter.KEY_ROUTE_NAME, FavsDbAdapter.KEY_STOPADDRESS, FavsDbAdapter.KEY_DEST_NAME};
		//String [] from = new String []{FavsDbAdapter.KEY_ROUTE_NAME, FavsDbAdapter.KEY_ROUTEID, FavsDbAdapter.KEY_STOPID};
		int [] to = new int[]{R.id.text1, R.id.text2, R.id.text3};
		SimpleCursorAdapter favs = new SimpleCursorAdapter(this, R.layout.favs_row, mFavCursor, from, to);
		setListAdapter(favs);
    }    
    
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
        Cursor c = mFavCursor;
        c.moveToPosition(position);
		//Toast.makeText(getApplicationContext(), "should delete the entry here", Toast.LENGTH_LONG);
        Intent i = new Intent(this, StopView.class);
        i.putExtra(Constants.sSTOP_KEY, c.getString(c.getColumnIndex(FavsDbAdapter.KEY_STOPID)));
        i.putExtra(Constants.sROUTE_KEY, c.getString(c.getColumnIndex(FavsDbAdapter.KEY_ROUTEID)));
        startActivity(i);
    }

}


